package uk.co.q3c.deplan.server.util.validation.rules;

import java.io.Serializable;

import javax.persistence.Id;

import uk.co.q3c.deplan.client.domain.AbstractDomainObject;
import uk.co.q3c.deplan.client.domain.DomainKind;
import uk.co.q3c.deplan.client.domain.DomainObject;
import uk.co.q3c.deplan.client.domain.resource.PersistenceReferenceException;
import uk.co.q3c.deplan.server.util.validation.ValidationResult;

import com.googlecode.objectify.Key;

public class IntegerExclude extends AbstractDomainObject implements Serializable, ValidationPropertyRule {

	private static final long serialVersionUID = -3247759650400915998L;
	int excludeValue;
	@Id
	private Long keyId;

	public IntegerExclude(int excludeValue) {
		super();
		this.excludeValue = excludeValue;
	}

	@Override
	public ValidationResult validate(String propertyName, Object propertyValue, ValidationResult vr) {
		vr.setValid(true);
		if (propertyValue instanceof Integer) {

			int value = (Integer) propertyValue;
			if (value == excludeValue) {
				vr.setValid(false);
				vr.addValidationMessage(propertyName + " cannot have a value of " + excludeValue);
			}
		} else {
			vr.addValidationMessage("Invalid class for validation rule");
			vr.setValid(false);
			return vr;
		}
		return vr;
	}

	@Override
	public DomainKind getKind() {
		return DomainKind.Validation;
	}

	public Long getKeyId() {
		return keyId;
	}

	@Override
	public Key<? extends DomainObject> getKey() throws PersistenceReferenceException {
		throw new RuntimeException("Not yet implemented");
	}

	@Override
	public void setKeyId(Long keyId) {
		throw new RuntimeException("Not yet implemented");
	}
}
